package com.tfjybj.itoo.exam.provider.service.impl;

import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.tfjybj.itoo.exam.entity.SigninDetailsEntity;
import com.tfjybj.itoo.exam.model.SigninDetailsModel;
import com.tfjybj.itoo.exam.provider.dao.SigninDetailsDao;
import com.tfjybj.itoo.exam.provider.service.SigninDetailsService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * SigninDetailsService接口实现类
 * ${base}表
 *
 * @author 郭满亮
 * @version 2.0.0
 * @since 2.0.0 2019-03-19 09:21:41
 */
@Service("signinDetailsService")
public class SigninDetailsServiceImpl extends BaseServicePlusImpl<SigninDetailsDao, SigninDetailsEntity> implements SigninDetailsService {
	
	//region 模板生成
    @Resource
    private SigninDetailsDao signinDetailsDao;
    @Resource
    private RedisTemplate redisTemplate;
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */
    /**
     * 根据签到表id查询所有学生签到情况
     * @param signInId 签到表id
     * @return 所有学生签到情况
     * @author 郭满亮
     * @since  2019年3月20日10:08:00
     */
    @Override
    public ItooResult queryAllSigninDetails(String signInId) {
        List<SigninDetailsModel> signinDetailsModels = signinDetailsDao.queryAllSigninDetails(signInId);
        if (signinDetailsModels.size() != 0){
            return ItooResult.build(ItooResult.SUCCESS, "查询成功",signinDetailsModels);
        }
        return ItooResult.build(ItooResult.SUCCESS,"查询失败",signinDetailsModels);
    }

    /**
     * 根据上课班id查询radis所有学生签到状态
     * @param teachClassId  上课班id
     * @return  查询结果
     * @author 郭满亮
     * @since 2019年3月20日11:34:36
     */
    @Override
    public ItooResult queryRadisStudentState(String teachClassId) {
        List<SigninDetailsEntity> signinDetailsEntities =
                (List<SigninDetailsEntity>) redisTemplate.opsForValue().get(teachClassId);
        if (signinDetailsEntities!=null){
            return ItooResult.build(ItooResult.SUCCESS,"查询成功",signinDetailsEntities);
        }
        return ItooResult.build(ItooResult.FAIL,"查询失败",signinDetailsEntities);
    }
    /**
     * 查询学生所在的上课班
     * @author 于云秀
     * @since 2019年3月20日
     * @param studentId
     * @return
     */
    @Override
   public List<SigninDetailsEntity> selectTeacherClass(String studentId){
       List<SigninDetailsEntity> signinDetailss  = signinDetailsDao.selectTeacherClass(studentId);
      return  signinDetailss;
   }

    /**
     * 更新考生状态
     * @author 于云秀
     * @since 2019年3月20日
     * @param studentId
     * @param state
     * @return
     */
    @Override
    public int updateStudentState(String courseId, String studentId, String state){

        int flag  = signinDetailsDao.updateStudentState(studentId,state);
        if (flag>0) {
            List<SigninDetailsEntity> signinDetailss  = signinDetailsDao.selectStudentState(courseId);
            redisTemplate.opsForValue().set(courseId,signinDetailss);
            redisTemplate.expire(courseId, 2400, TimeUnit.SECONDS);
            return flag;
        }
        return  flag;
    }

    /**
     * 查询学生状态
     * @author 于云秀
     * @since 2019年3月24日
     * @param courseId
     * @param studentId
     * @return
     */
    @Override
   public  List<SigninDetailsEntity> selectState(String courseId, String studentId){
        List<SigninDetailsEntity> selectState =  signinDetailsDao.selectState(courseId,studentId);
        return selectState;
    }
}
